package com.dk.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dk.entity.DtItem;
import com.dk.service.EsService;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EsServiceImpl implements EsService {
    @Autowired
    private RestHighLevelClient client;
    @Override
    public Map<String, Object> getWord(String title, Integer pageNumber, Integer pageSize) {
        //设置请求对象
        SearchRequest request = new SearchRequest("dt_item");
        SearchSourceBuilder builder = new SearchSourceBuilder();
        MatchQueryBuilder matchQuery = QueryBuilders.matchQuery("title", title);


        builder.query(matchQuery);
        builder.sort("id");//默认升序
        //高亮设置
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.field("title");
        highlightBuilder.preTags("<font color='red'>");
        highlightBuilder.postTags("</font>");
        //分页
        builder.from(pageNumber);
        builder.size(pageSize);
        builder.highlighter(highlightBuilder);
        builder.highlighter(highlightBuilder);
        request.source(builder);
        Map<String,Object> map = new HashMap<>();
        List<DtItem> list = new ArrayList<>();
        long totals = 0;
        try {
            SearchResponse response = client.search(request, RequestOptions.DEFAULT);
            SearchHit[] hits = response.getHits().getHits();
             totals = response.getHits().getTotalHits();
            for (SearchHit hit : hits) {
                //将获取内容转化为对象
                DtItem item = JSONObject.parseObject(hit.getSourceAsString(), DtItem.class);
                //获取高亮字段
                Map<String, HighlightField> highlightFields = hit.getHighlightFields();
                HighlightField highlightField = highlightFields.get("title");
                Text[] fragments = highlightField.getFragments();
                //获取高亮字段给实体类
                for (Text fragment : fragments) {
                    item.setTitle(fragment.toString());
                }
                list.add(item);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        map.put("items",list);
        map.put("title",title);
        map.put("totals",totals);
        return map;
    }

    @Override
    public Map<String, Object> getNearbyByWord(String adress, Integer pageNumber, Integer pageSize) {
        //设置请求对象
        SearchRequest request = new SearchRequest("dt_item");
        SearchSourceBuilder builder = new SearchSourceBuilder();
        MatchQueryBuilder matchQuery = QueryBuilders.matchQuery("adress", adress);
        builder.query(matchQuery);
        builder.sort("id");//默认升序
        //高亮设置
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.field("adress");
        highlightBuilder.preTags("<font color='red'>");
        highlightBuilder.postTags("</font>");
        //分页
        builder.from(pageNumber);
        builder.size(pageSize);
        builder.highlighter(highlightBuilder);
        builder.highlighter(highlightBuilder);
        request.source(builder);
        Map<String,Object> map = new HashMap<>();
        List<DtItem> list = new ArrayList<>();
        long totals = 0;
        try {
            SearchResponse response = client.search(request, RequestOptions.DEFAULT);
            SearchHit[] hits = response.getHits().getHits();
            totals = response.getHits().getTotalHits();
            for (SearchHit hit : hits) {
                //将获取内容转化为对象
                DtItem item = JSONObject.parseObject(hit.getSourceAsString(), DtItem.class);
                //获取高亮字段
                Map<String, HighlightField> highlightFields = hit.getHighlightFields();
                HighlightField highlightField = highlightFields.get("adress");
                Text[] fragments = highlightField.getFragments();
                //获取高亮字段给实体类
                for (Text fragment : fragments) {
                    item.setTitle(fragment.toString());
                }
                list.add(item);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        map.put("items",list);
        map.put("adress",adress);
        map.put("totals",totals);
        return map;
    }
}
